﻿using System;

class Spiral
{
    static void Main()
    {
        //Exercise 14. *
        //Write a program that reads a positive integer number N (N < 20) from console and outputs in the console 
        //the numbers 1 ... N numbers arranged as a spiral.

        Console.Write("Enter N  (N<20):  ");
        int n = int.Parse(Console.ReadLine());
        int[,] matrix = new int[n,n];
        int counter = 1;
        int maxRow = n - 1;
        int maxCol = n - 1;
        int row = 0;
        int col = 0;       

        while (counter <= n * n)
        {
            for (int i = col; i <= maxCol; i++)          //1-st row
            {
                matrix[row, i] = counter;
                counter++;
            }
            row++;                                      // go to next row

            for (int i = row; i <= maxRow; i++)         // last col
            {
                matrix[i, maxCol] = counter;
                counter++;
            }
            maxCol--;

            for (int i = maxCol; i >= col; i--)         //last row
            {
                matrix[maxRow, i] = counter;
                counter++;
            }
            maxRow--;

            for (int i = maxRow; i >= row; i--)         //1-st col
            {
                matrix[i, col] = counter;
                counter++;
            }
            col++;                                      //next cicle should start from the next col
        }
              

        for (int rows = 0; rows < matrix.GetLength(0); rows++)
        {
            for (int cols = 0; cols < matrix.GetLength(1); cols++)
            {
                Console.Write(matrix[rows,cols] + " ");
            }
            Console.WriteLine();
        }
    }
}

